<template>
  <div>
    <el-card shadow="never">
      <el-button :disabled="loading" @click="addAdvert">添加广告</el-button>
    </el-card>
    <el-card shadow="never" :body-style="{ padding: '0px 0px' }">
      <el-table border :data="adverts" style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="id" width="150" align="center">
        </el-table-column>
        <el-table-column prop="name" label="广告名称" align="center">
        </el-table-column>
        <el-table-column
          prop="spaceId"
          label="广告位置"
          align="center"
          width="200"
        >
          <template slot-scope="scope">
            {{ getSpacePosition(scope.row) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="phone"
          label="广告图片"
          align="center"
          width="120"
        >
          <template slot-scope="scope">
            <img height="80px" :src="scope.row.img" />
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="时间"
          align="center"
          width="250"
        >
          <template slot-scope="scope">
            <div>开始时间：{{ scope.row.endTimeFormatString }}</div>
            <div>结束时间：{{ scope.row.startTimeFormatString }}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="status"
          label="上下线"
          align="center"
          width="120"
        >
          <template slot-scope="scope">
            <el-switch
              :value="scope.row.status"
              active-color="#40586f"
              inactive-color="#dcdfe6"
              :active-value="1"
              :inactive-value="0"
              @change="updateStatus(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          prop="address"
          label="操作"
          align="center"
          width="180px"
        >
          <template slot-scope="scope">
            <el-button type="text" @click="editAdvert(scope.row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getAdvertPages, updateStatus } from '@/services/advert.ts'
import { getAllSpaces } from '@/services/advert-space'
export default Vue.extend({
  name: 'AdvertList',
  data () {
    return {
      adverts: [],
      spaces: [],
      loading: false,
      page: 1,
      size: 1000,
      total: 0
    }
  },
  created () {
    this.loadAdverts()
    this.loadAdvertSpaces()
  },
  methods: {
    async loadAdverts () {
      const { data } = await getAdvertPages({
        pageNum: this.page,
        pageSize: this.size
      })

      this.adverts = data.content
      this.total = data.content.length
    },

    async loadAdvertSpaces () {
      const { data } = await getAllSpaces()
      this.spaces = data.content
    },

    getSpacePosition (advert: FromServer.AdvertItem) {
      const space = this.spaces.find(
        (item: FromServer.AdvertSpaceItem) => item.id === advert.spaceId
      )
      if (space) {
        return (space as any).name
      }
    },

    updateStatus (advert: FromServer.AdvertItem) {
      this.$confirm('是否修改上/下线状态?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await updateStatus(advert.id, advert.status === 1 ? 0 : 1)

          this.loadAdverts()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          })

          this.loadAdverts()
        })
    },

    addAdvert () {
      this.$router.push({
        name: 'advert-create'
      })
    },
    editAdvert (advert: FromServer.AdvertItem) {
      this.$router.push({
        name: 'advert-edit',
        params: {
          id: advert.id.toString()
        }
      })
    }
  }
})
</script>

<style lang="scss" scoped>
</style>
